package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.utils.poi.EasyexcelUtils;
import com.ruoyi.system.domain.excel.SysDictDataExcel;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }


    @Log(title = "导出字典数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        List<SysDictDataExcel> excelList = new ArrayList<>();

        for (SysDictData sysDictData : list) {
            SysDictDataExcel excel = new SysDictDataExcel();
            excel.setDictCode(sysDictData.getDictCode());
            excel.setDictSort(sysDictData.getDictSort());
            excel.setDictLabel(sysDictData.getDictLabel());
            excel.setDictValue(sysDictData.getDictValue());
            excel.setDictType(sysDictData.getDictType());

            // 转换是否默认字段为显示文本
            String isDefault = sysDictData.getIsDefault();
            if ("Y".equals(isDefault)) {
                excel.setIsDefault("是");
            } else if ("N".equals(isDefault)) {
                excel.setIsDefault("否");
            } else {
                excel.setIsDefault(isDefault);
            }

            // 使用字典服务将账号状态代码转换为文本描述
            if (StringUtils.isNotEmpty(sysDictData.getStatus())) {
                String statusText = dictDataService.selectDictLabel("sys_normal_disable", sysDictData.getStatus());
                excel.setStatus(StringUtils.isNotEmpty(statusText) ? statusText : sysDictData.getStatus());
            } else {
                excel.setStatus(sysDictData.getStatus());
            }

            excel.setCreateBy(sysDictData.getCreateBy());
            excel.setCreateTime(sysDictData.getCreateTime());
            excel.setRemark(sysDictData.getRemark());
            excelList.add(excel);
        }

        // 使用EasyExcel导出
        EasyexcelUtils.exportExcel(response, excelList, "字典数据", SysDictDataExcel.class);
    }
    /**
     * 查询字典数据详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable(value = "dictCode") Long dictCode)
    {
        return success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable(value = "dictType") String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return success(data);
    }

    /**
     * 根据字典类型和值获取字典标签
     */
    @GetMapping(value = "/label/{dictType}/{dictValue}")
    public R<String> getDictLabel(@PathVariable(value = "dictType") String dictType, 
                                 @PathVariable(value = "dictValue") String dictValue)
    {
        String dictLabel = dictDataService.selectDictLabel(dictType, dictValue);
        return R.ok(dictLabel);
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "新增字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictData dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @Log(title = "修改字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "删除字典数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable(value = "dictCodes") Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
